<?php
define('IN_SCRIPT', 1);
define('HESK_PATH', './');
define('HESK_NO_ROBOTS', 1);

/* Get all the required files and functions */
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');
require(HESK_PATH . 'inc/database.inc.php');

hesk_session_start();

/* Connect to database */
hesk_dbConnect();

if (isset($_GET['category'])) {
    $category_name = $_GET['category'];

    /* Get start node */
    $sql = "SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "diag_categories` WHERE `name` = '" . $category_name . "';";
    $result = hesk_dbQuery($sql);

    $category = hesk_dbFetchAssoc($result);
    $node = $category['start_node'];
} else {
    $node = $_GET['answer'];
}

$question_id = $_GET['question_id'];

$sql = "SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "diag_nodes` WHERE `token` = '" . $node . "';";
$result = hesk_dbQuery($sql);

$node_result = hesk_dbFetchAssoc($result);
$content = $node_result['content'];
$yes_node = $node_result['yes_node'];
$no_node = $node_result['no_node'];
$type = $node_result['type'];

if ($type == 0) {
    ?>
    <p><?php echo $content ?></p>
    <p>
        <input type="submit" onmouseout="hesk_btn(this,'orangebutton');" onmouseover="hesk_btn(this,'orangebuttonover');" onclick="submitAnswer('<?php echo $question_id; ?>','<?php echo $yes_node; ?>'); return false;" class="orangebutton" value="<?php echo $hesklang['yes']; ?>">
        <input type="submit" onmouseout="hesk_btn(this,'orangebutton');" onmouseover="hesk_btn(this,'orangebuttonover');" onclick="submitAnswer('<?php echo $question_id; ?>','<?php echo $no_node; ?>'); return false;" class="orangebutton" value="<?php echo $hesklang['no']; ?>">
    </p>
    <?php
} else {
    $query = $content;

    hesk_dbConnect();

    /* Get relevant articles from the database */
    $sql = 'SELECT * FROM `' . hesk_dbEscape($hesk_settings['db_pfix']) . 'kb_articles` WHERE `type`=\'0\' AND MATCH(`subject`,`content`) AGAINST (\'' . hesk_dbEscape($query) . '\') LIMIT 5';
    $res = hesk_dbQuery($sql);
    $num = hesk_dbNumRows($res);
    ?>
    <p style="font-weight: bold">Solusi</p>
    <p><?php echo $content ?></p><br />
    <p style="font-weight: bold">Artikel Terkait (Knowledgebase)</p>
    <p>
        <?php
        while ($article = hesk_dbFetchAssoc($res)) {
            $txt = strip_tags($article['content']);
            if (strlen($txt) > $hesk_settings['kb_substrart']) {
                $txt = substr(strip_tags($article['content']), 0, $hesk_settings['kb_substrart']) . '...';
            }

            echo '
			<a href="' . $hesk_settings['site_url'] . '/knowledgebase.php?article=' . $article['id'] . '" target="_blank">' . $article['subject'] . '</a>
		    <br />';
        }
        ?>
    </p>
    <?php if (isset($_GET['mode'])): ?>
        <p>
            <input type="submit" onmouseout="hesk_btn(this,'orangebutton');" onmouseover="hesk_btn(this,'orangebuttonover');" onclick="publishStream(); return false;" class="orangebutton" value="Share Result">
            <input type="submit" onmouseout="hesk_btn(this,'orangebutton');" onmouseover="hesk_btn(this,'orangebuttonover');" onclick="inviteFriends(); return false;" class="orangebutton" value="Invite Friends">
        </p>
    <?php endif ?>
<?php } ?>
